package com.qiaofang.service.impl;

import com.qiaofang.common.constant.ExceptionEnum;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.PageDTO;
import com.qiaofang.common.model.UiListBean;
import com.qiaofang.dto.PremisesDynamicDTO;
import com.qiaofang.entity.PremisesDynamic;
import com.qiaofang.entity.PremisesDynamicExternal;
import com.qiaofang.mapper.PremisesDynamicMapper;
import com.qiaofang.repository.PremisesDynamicExternalRepository;
import com.qiaofang.repository.PremisesDynamicRepository;
import com.qiaofang.service.PremisesDynamicService;
import com.qiaofang.vo.PremisesDynamicExternalVO;
import com.qiaofang.vo.PremisesDynamicVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PremisesDynamicServiceImpl implements PremisesDynamicService {

    @Autowired
    private PremisesDynamicMapper premisesDynamicMapper;
    @Autowired
    private PremisesDynamicRepository premisesDynamicRepository;
    @Autowired
    private PremisesDynamicExternalRepository dynamicExternalRepository;

    @Override
    public UiListBean listPremisesDynamic(PremisesDynamicDTO dto, PageDTO page) {
        UiListBean<PremisesDynamicVO> uiListBean = new UiListBean();
        List<PremisesDynamic> premisesDynamicList = premisesDynamicMapper.listPremisesDynamic(dto, page);
        List<PremisesDynamicVO> voList = new ArrayList<>();
        if (null == premisesDynamicList || premisesDynamicList.size() == 0) {
            return uiListBean;
        }
        for (PremisesDynamic premisesDynamic : premisesDynamicList) {
            PremisesDynamicVO vo = new PremisesDynamicVO();
            BeanUtils.copyProperties(premisesDynamic, vo);
            voList.add(vo);
        }
        uiListBean.setList(voList);
        uiListBean.setTotalCount(premisesDynamicMapper.listPremisesDynamicCount(dto));
        return uiListBean;
    }

    @Override
    public PremisesDynamic getDynamicDetail(Long id) {
        PremisesDynamic premisesDynamic = premisesDynamicRepository.findById(id)
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        if('1' == premisesDynamic.getCanSee()){
            premisesDynamic.setCanSee('是');
        }else {
            premisesDynamic.setCanSee('否');
        }
        if (premisesDynamic != null) {
            try {
                if (null != premisesDynamic.getRemark()){
                    premisesDynamic.setRemark(URLDecoder.decode(premisesDynamic.getRemark(), "utf-8"));
                }
                if (null != premisesDynamic.getContent()) {
                    premisesDynamic.setContent(URLDecoder.decode(premisesDynamic.getContent(), "utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                log.error("URLDecoder decode error: {}", e);
            }
        }
        return premisesDynamic;
    }

    @Override
    public List<PremisesDynamicExternalVO> findBindPremisesDynamic(Long dynamicId) {
        List<PremisesDynamicExternal> external = dynamicExternalRepository.listDynamicExternal(dynamicId);
        List<PremisesDynamicExternalVO> voList = new ArrayList<>();
        if (null == external || external.size() == 0) {
            return voList;
        }
        for (PremisesDynamicExternal dynamicExternal : external) {
            PremisesDynamicExternalVO vo = new PremisesDynamicExternalVO();
            BeanUtils.copyProperties(dynamicExternal, vo);
            voList.add(vo);
        }
        return voList;
    }

}
